package com.kdtech.analyse.outside;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.kdtech.analyse.AnalyseNews;
import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;

import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.RegexUtils;
import com.kdtech.utils.StringUtils;

/**
 * 新唐人
 * @author KK
 *
 */
public class WashengNewsAnalyse implements AnalyseNews {

	private static final String[] regex={
		"http://washeng.net/[A-Za-z]*/[A-Z]*/[a-z]*/[a-z]*/[0-9]*.shtml"
	};


	
	public boolean isDetailPage(String url) {
		return RegexUtils.matchAnyIgnoreCase(url, regex);
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();

		news.setUrl(url);
		String title=null;
		String content=null;
		String author=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);

		if (url.startsWith("http://www.boxun.com/news") || url.startsWith("http://news.boxun.com")){
			title =doc.select("h2").text();
			content=HtmlCleaner.getContentHtml(url,doc.select("u1"));
			date=DateUtils.matchDate(doc.select("u1").text());
			author=JSoupUtils.matchAuthor(doc, "来源：");
		}

		if (url.startsWith("http://www.boxun.com/forum")){
			title =doc.select("title").text();
			content=HtmlCleaner.getContentHtml(url,doc.select("td.F11 p"));
			String dateStr = doc.select("p:containsOwn(于 北京时间)").text();
			dateStr = StringUtils.substringAfter(dateStr, "北京时间 ");
			if(dateStr!=null){
				date = DateUtils.parseLong(dateStr, "MM/dd/yyyy");
			}
//			date=JSoupUtils.matchDate(doc, "送交者");
			author=JSoupUtils.matchAuthor(doc, "送交者:");
		}
		if (url.startsWith("http://blog.boxun.com")){
			title =doc.select("title").text();
			Element first=doc.select("#disqus_thread").first();
			if (first!=null){
				content=HtmlCleaner.getContentHtml(url,first.parent());
			}
			if(StringUtils.isBlank(content)) content=HtmlCleaner.getContentHtml(url,doc.select("p"));
			date=JSoupUtils.matchDate(doc, "时间:");
			if (date==null){
				date=DateUtils.matchDate(StringUtils.substringBetween(content, "（","发表）"));
			}
			author=StringUtils.substringAfter(title, "/");
			if(StringUtils.isBlank(author)) author=StringUtils.substringBefore(title, "：");
		}
		news.setTitle(StringUtils.trimSpace(title));
		news.setContent(content);
		news.setAuthor(author);
		news.setDate(date);

		return news;
	}

	

	

	
}
